package com.example.ruralservicedigitalplatform.controller.villager;

import com.example.ruralservicedigitalplatform.comment.Result;
import com.example.ruralservicedigitalplatform.domain.dto.ShoppingCartDto;
import com.example.ruralservicedigitalplatform.domain.po.ShoppingCart;
import com.example.ruralservicedigitalplatform.service.ShoppingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/villager/shoppingCart")
@Tag(name = "购物车相关接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查看购物车
     * @return
     */
    @Operation(summary = "查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        List<ShoppingCart> list = shoppingCartService.showShoppingCart();
        return Result.success(list);
    }

    /**
     * 添加购物车
     * @param shoppingCartDto
     * @return
     */
    @Operation(summary = "添加购物车")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDto shoppingCartDto){
        shoppingCartService.addShoppingCart(shoppingCartDto);
        return Result.success();

    }

    /**
     * 清空购物车
     * @return
     */
    @Operation(summary = "清空购物车")
    @DeleteMapping("/clean")
    public Result clean(){
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

    /**
     * 删除购物车的一个商品
     * @param shoppingCartDto
     * @return
     */
    @Operation(summary = "删除购物车的一个商品")
    @PostMapping("/sub")
    public Result delete(@RequestBody ShoppingCartDto shoppingCartDto){
        shoppingCartService.deleteOne(shoppingCartDto);
        return Result.success();
    }
}
